package be.xios.projectquiz.master;

import java.io.IOException;
import java.net.ConnectException;
import java.net.Socket;
import java.net.UnknownHostException;
import java.util.Date;
import java.sql.Timestamp;

import javax.swing.JFrame;
import javax.swing.JOptionPane;

import be.xios.projectquiz.game.Game;
import be.xios.projectquiz.game.GameMasterGUI;
import be.xios.projectquiz.global.CardsViewer;
import be.xios.projectquiz.global.ScreenMethods;
import be.xios.projectquiz.global.SharedItems;
import be.xios.projectquiz.global.StreamMethods;
import be.xios.projectquiz.profiles.Master;

/// <summary>
/// Deze klasse verzorgt de connectie tussen master en server
/// </summary>
/// <author>Sebastian Kubski</author>
public class MasterClient implements Runnable {
	
	private String ip;
	private SharedItems shared;
	private Master master = new Master();
	private Game currentGame;
	private Socket socket;
	private StreamMethods streamMethods;
	private CardsViewer viewer;
	private Date date;
	private GameMasterGUI gameGUI;
	
	//CONSTRUCTOR
	public MasterClient(String ip, SharedItems shared, CardsViewer viewer) {
		this.ip=ip;
		this.shared=shared;
		this.viewer=viewer;
		this.date=new Date();
	}
	
	//START RUN
	@Override
	public void run() {
		// PlayerClient info op boot
		String logString = "[" + new Timestamp(date.getTime())
				+ "] CLIENTSIDE: " + Thread.currentThread().getName();
		System.out.println(logString + " is " + master.getName());
		try {
			socket = new Socket(ip, 5000);
			streamMethods = new StreamMethods(socket);
			shared.veranderConnectStatus("Connected with the server");
			shared.enableConnect(false);
			streamMethods.openObjectOutputStream();
            streamMethods.getObjectOutput().writeObject(master);
            System.out.println("Ik heb mijn profile als master verzonden");
        	viewer.show("LP");   
		}
		catch (UnknownHostException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		catch(ConnectException ex){
        	JOptionPane.showMessageDialog(null,"Kan niet verbinden met de server. \t | \t Cannot connect with the server. \t","Waarschuwing | Warning",JOptionPane.ERROR_MESSAGE);
        }
		catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

		while(shared.getGame()== null) {
			try {
				Thread.sleep(2000);
				streamMethods.openObjectInputStream();

				currentGame = (Game) streamMethods.getObjectInput().readObject();
				
				shared.setGame(currentGame, master.getName()); 
				currentGame = null;
			
			}
			catch (InterruptedException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		
		while (currentGame == null) {
			System.out.println(logString + master.getName()
					+ " is waiting for an updated game");
			try {
				streamMethods.openObjectInputStream();
				currentGame = (Game) streamMethods.getObjectInput()
						.readObject();
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}

			if (currentGame != null) {
				// origineel frame met de keuzes en login invisible maken
				shared.houdiniOriginalFrame(false);

				System.out.println(logString + " opende de GAMEPLAYERGUI");
				ScreenMethods main = new ScreenMethods();

				gameGUI = new GameMasterGUI(currentGame);
				gameGUI.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
				gameGUI.setSize(main.getFullScreenX(), main.getFullScreenY());
				gameGUI.setUndecorated(true);
				gameGUI.setVisible(true);
			}
		}
		
		while (!currentGame.isEnded()) {
			try {
				streamMethods.openObjectInputStream();
				currentGame = (Game) streamMethods.getObjectInput()
						.readObject();
				gameGUI.setCurrentGame(currentGame);
			} catch (ClassNotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}// EINDE isEnded()
	} //EINDE RUN
}